﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageSystem.Dialog
{
    public partial class DialogNode : Form
    {
        private TextBox _retText = null;
        private NODE _nodeWhere = null;
        public DialogNode(TextBox text, NODE nodeWhere)
        {
            if (text == null)//没有传入NODE时关闭窗口
            {
                MessageBox.Show("传入参数为空");
                Close();
            }
            _retText = text;
            _nodeWhere = nodeWhere;

            InitializeComponent();
        }
        //设置listview
        private void SetListView(ListView view, NODE node)
        {
            if (view == null || node == null) return;
            ListViewItem item = new ListViewItem();
            item.Text = node.ID.ToString();

            item.SubItems.Add(node.NAME);
            item.SubItems.Add(node.CONTEXT);
            if (node.FLOW_ID != null)
                item.SubItems.Add(node.FLOW_ID.ToString());
            else
                item.SubItems.Add("");

            if (node.LAST_ID != null)
                item.SubItems.Add(node.LAST_ID.ToString());
            else
                item.SubItems.Add("");

            if (node.NEXT_ID != null)
                item.SubItems.Add(node.NEXT_ID.ToString());
            else
                item.SubItems.Add("");

            if (node.ERROR_ID != null)
                item.SubItems.Add(node.ERROR_ID.ToString());
            else
                item.SubItems.Add("");

            if (node.FLAG != null)
                item.SubItems.Add(node.FLAG.ToString());
            else
                item.SubItems.Add("");

            view.Items.Add(item);
        }
        //清空list view
        private void ClearListView(ListView view)
        {
            view.Items.Clear();
        }
        //加载工序信息
        private void DialogNode_Load(object sender, EventArgs e)
        {
            if (_nodeWhere == null) _nodeWhere = new NODE();

            var selectNode = MethodWcf.SelectNodeClient(_nodeWhere);
            if (selectNode.result)
            {
                for(int i = 0; i < selectNode.value.Count; i++)
                {
                    SetListView(this.listViewNode, selectNode.value[i]);
                }
            }
            else//未能获得信息则关闭这个界面
            {
                MessageBox.Show(selectNode.message);
                this.Close();
            }
        }
        //返回选择的NODE
        private void listViewNode_MouseClick(object sender, MouseEventArgs e)
        {
            var choiceId = this.listViewNode.SelectedItems[0].Text;//选择的ID

            _retText.Text = choiceId;

            this.Close();
        }
    }
}
